func greet(person:String,day:String) -> String{
	print("hello \(person),today is \(day)")
	
	return "hello \(person),today is \(day)"
}

//方法调用第一个参数不写参数名，后面的全部要写。特殊情况是尾闭包，_来强制取消参数名
//greet(person:"Andy",day:"2017-01-18") 
greet("Andy",day:"2017-01-18") 
greet(_ :"Andy",day:"2017-01-18") 

//greet("Andy",_ :"2017-01-18") 



var finonacci = [1,1,2,3,5,8]
//返回数据元组
func calculateStatistics(score:[Int]) 
						-> (min:Int,max:Int,sum:Int)
{
	
	var min = finonacci[0] 
	var max = finonacci[0] 
	var sum = 0
	
	for num in finonacci{
		if num < min{
			min = num
		}
		
		if num >= max {
			max = num
		}
		sum += num
	}
	
	return (min,max,sum)
	
}

var metaData = calculateStatistics(finonacci)

print("min is \(metaData.0)")
print("max is \(metaData.1)")
print("sum is \(metaData.2)")
print("sum is \(metaData.sum)")



func sunOfArray(numbers:Int...) ->Int{
	var total = 0 
	for n in numbers{
		total += n
	}
	
	return total
}

print("total of sum \(sunOfArray(12,98,1))")
print("total is \(sunOfArray())")


//函数里添加函数
func returnFifteen() -> Int{
	var y = 10 
	func add(){
		y += 5
	}
	
	//add()
	return y
}

print("\(returnFifteen())")

//函数返回函数

func makeIncrementer() -> ((Int) -> Int) {
	func addOne(number:Int) -> Int{
		return 1 + number
	}
	
	return addOne
}

print("\(makeIncrementer()(2))")//3

var increment = makeIncrementer()

print("increment is \(increment(2))")//3

//函数作为参数

func hasAnyMatches(list:[Int],condition:(Int) -> Bool) -> Bool
{
	for item in list{
		if condition(item){
			return true
		}
	}
	return false
}

func hasConditionFour(num:Int) -> Bool{
	return num == 4
}

func hasMoreThanEight(num:Int) -> Bool{
	return num >= 8
}

var isContain = hasAnyMatches(finonacci,condition:hasConditionFour)
print("数组里面可有4？ \(isContain)")
isContain = hasAnyMatches(finonacci,condition:hasMoreThanEight)
print("数组里面可又大于8的数？ \(isContain)")

//自定义闭包
var isMoreThanSeven = {
		(num:Int) -> Bool in  return num > 7
		
}
print("isMoreThanSeven: \(isMoreThanSeven(6))")
isMoreThanSeven = {
		num -> Bool in  return num > 7
		
}
print("isMoreThanSeven: \(isMoreThanSeven(4))")
isMoreThanSeven = {
		return $0 > 7
}

print("isMoreThanSeven: \(isMoreThanSeven(10))")

func sorted(a:Int,b:Int) -> Bool{
	return a > b
}

var sortedCloselure = {
	(b:[Int]) -> [Int] in 
				var temp = b[0]
				//var cont:Int = b?.count as Int
				var tempArray = b;
				for var i in tempArray.enumerate(){
					print(" i = \(i.index)")
					//i += 10
					
					var index_:Int = i.index
					tempArray[index_] += 2
					
				}
				
				return tempArray
}

var sortedCloselureResult = sortedCloselure(finonacci)

print("sortedCloselureResult is \(sortedCloselureResult)")


//函数闭包
/* 闭包 表达式
{ (parameters) -> returnType in
	statements
} 
*/
//数组finonacci 每个数＊3
var n = finonacci.map(
	{
		(number : Int) -> Int in let result = 3 * number
		return result
	}
)

print(n)

n = finonacci.map(
	{number in 3 * number}
)
print(n)

//倒序排列，前面的数大于后面的数
n = finonacci.sort {$0 > $1}
print(n)



let names:Set = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] 

//func backwards(s1: [String], s2: String) -> Bool {
//	return s1 > s2
//}


var reversed = names.sort({$0 > $1})

print(reversed)

//names.sorted()

print(names)










						
